package com.pwser.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Assert;
import org.junit.Test;

public class SimpleExample {
	@Test
	public void testHelloWord() {
		//获取IniSecurityManagerFactory工厂，使用ini文件初始化
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//得到SecurityManager实例，并绑定给SecurityUtils
		SecurityManager securityManager = factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		//得到Subject及创建用户名/密码身份验证Token
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123");
		try {
			//登录的身份验证
			subject.login(token);
			System.out.println("登录成功！");
		} catch (UnknownAccountException e) {
			System.out.println("帐号不存在. There is no user with username of " + token.getPrincipal());
		} catch (IncorrectCredentialsException e) {
			System.out.println("登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.");
		} catch (ExcessiveAttemptsException e) {
			System.out.println("登录失败次数过多. Password for account " + token.getPrincipal() + " was incorrect.");
		} catch (LockedAccountException e) {
			System.out.println("帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.");
		} catch (DisabledAccountException e) {
			System.out.println("帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.");
		} catch (ExpiredCredentialsException e) {
			System.out.println("帐号已过期. the account for username " + token.getPrincipal() + "  was expired.");
		} catch (UnauthorizedException e) {
			System.out.println("您没有得到相应的授权！" + e.getMessage());
		}  
		Assert.assertEquals(true, subject.isAuthenticated());
		//登出
		subject.logout();
	}
	
	@Test
	public void testJDBC() {
		//获取IniSecurityManagerFactory工厂，使用ini文件初始化
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-jdbc.ini");
		//得到SecurityManager实例，并绑定给SecurityUtils
		SecurityManager securityManager = factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		//得到Subject及创建用户名/密码身份验证Token
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("wangwu", "b6f8daff78cfcb486454e6914e666c71");
		try {
			//登录的身份验证
			subject.login(token);
			System.out.println("登录成功！");
			Assert.assertEquals(true, subject.isAuthenticated());
			//使用Shiro自带的断言判断用户是否有被授权
			subject.checkRole("普通用户");
			subject.checkPermission("系统管理");
		} catch (UnknownAccountException e) {
			System.out.println("帐号不存在. There is no user with username of " + token.getPrincipal());
		} catch (IncorrectCredentialsException e) {
			System.out.println("登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.");
		} catch (ExcessiveAttemptsException e) {
			System.out.println("登录失败次数过多. Password for account " + token.getPrincipal() + " was incorrect.");
		} catch (LockedAccountException e) {
			System.out.println("帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.");
		} catch (DisabledAccountException e) {
			System.out.println("帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.");
		} catch (ExpiredCredentialsException e) {
			System.out.println("帐号已过期. the account for username " + token.getPrincipal() + "  was expired.");
		} catch (UnauthorizedException e) {
			System.out.println("您没有得到相应的授权！" + e.getMessage());
		}  
		//登出
		subject.logout();
	}

}
